package it.terry.pressione;

import java.text.DecimalFormat;

import android.app.Activity;
import android.content.Context;
import android.hardware.SensorManager;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

import it.terry.pressione.hardware.Sensor;
import it.terry.pressione.hardware.SensorEvent;
import it.terry.pressione.hardware.SensorEventListener;
import it.terry.pressione.hardware.SensorManagerSimulator;



public class SimulationActivity extends Activity {
	@SuppressWarnings("unused")
	private SensorManagerSimulator mSensorManager;
	
	private float [] mValues = new float[1];
	private Context ctx;
	private int DELAY_MS_FASTEST = 0;
	private int DELAY_MS_GAME = 20;
	private int DELAY_MS_UI = 60;
	private int DELAY_MS_NORMAL = 200;
	private Sensor sensor;
	private TextView txtX;
	private Listener mListener;
	public Handler handler;
	DecimalFormat mDecimalFormat;
	
	String[] mDelayTypes = new String[] { "Fastest", "Game", "UI", "Normal" };
	int[] mDelayValue = new int[] {
			SensorManager.SENSOR_DELAY_FASTEST,
			SensorManager.SENSOR_DELAY_GAME, 
			SensorManager.SENSOR_DELAY_UI, 
			SensorManager.SENSOR_DELAY_NORMAL};
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        ctx = this.getApplicationContext();
        txtX = (TextView) findViewById(R.id.txtX);
     
        mSensorManager = SensorManagerSimulator.getSystemService(this, SENSOR_SERVICE);
        sensor = new Sensor(ctx,5);
        registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
         
        
        
        
    	}
    
    private class MyHandler extends Handler{
		@Override
		public void handleMessage(Message msg) {
			
			Bundle bundle = msg.getData();
			if(bundle.containsKey("refresh")) {
		        mValues = bundle.getFloatArray("refresh");
		        SensorEvent event = new SensorEvent(ctx, mValues, 5);
		        Log.d("SIMU", "valori evento"+event.type+ ""+event.values[0]+" ");
		        mListener.mSensorListener.onSensorChanged(event);
		        
		      }
				
				
		}
    };
				
    private class Listener
	{

		
		private SensorEventListener mSensorListener;
		private Sensor mSensor;
		
	    @SuppressWarnings("unused")
		private int mDelay;
	    @SuppressWarnings("unused")
		private long mNextUpdateTime;
	    
	    //Costruttore
	    Listener(SensorEventListener listener, Sensor sensor, int delay)
	    {

	    	mSensorListener = listener;
	    	mSensor = sensor;
	    	mSensor.enable_pres();
	    	mDelay = delay;
	    }
	}
	
	
	protected boolean registerListener(SensorEventListener listener, Sensor sensor, int rate){
		
		 int delay = -1;
	        
	        //here we check the sensor rate that is going to be applied to listener
	        switch(rate)
	        {
	        case SensorManager.SENSOR_DELAY_FASTEST:
	            delay = DELAY_MS_FASTEST;
	            break;

	        case SensorManager.SENSOR_DELAY_GAME:
	            delay = DELAY_MS_GAME;
	            break;

	        case SensorManager.SENSOR_DELAY_UI:
	            delay = DELAY_MS_UI;
	            break;

	        case SensorManager.SENSOR_DELAY_NORMAL:
	            delay = DELAY_MS_NORMAL;
	            break;

	        default:
	            return false;
	        }
	        boolean result;
	        
	                mListener = new Listener(listener,sensor,delay);
	        		handler = new MyHandler();
	        		new Autocordsender(ctx, handler).start();
	        		result = sensor.enable_pres();	
	        		
	      	 
	        return result;
	
	}
	
	protected void unregisterListener(SensorEventListener listener, Sensor sensor) {
	   
	}
        
private SensorEventListener listener = new SensorEventListener(){
		
		/**
		 * onAccuracyChanged must be added, but it doesn't need any editing.
		 */
		@Override
		public void onAccuracyChanged(Sensor sensor, int acc){
		}
		
		/**
		
		 * }{[][
		 * onSensorChanged method is used to write events of our enabled sensors
		
		 * in our application.
		 */
		@Override
		public void onSensorChanged(SensorEvent event){
			@SuppressWarnings("unused")
			int sensor = event.type;
			float[] values = event.values;
			
				 if(event.type == 5){
					 
	            // Dalla documentazione:
	            // All values are in SI units (m/s^2) and  measure the
	            // acceleration applied to the phone minus the force of gravity.
					 Log.d("SIMU", "E' stato scatenato l'evento");
					 Log.d("SIMU", "Valore X: "+event.values[0]);
					Log.d("SIMU", "Visualizzo a video il valore");
					 
					txtX.setText("X: " + values[0]);
	 
	           
				 }
	    }
};          	
}